use dta/SMDLevel.dta, clear

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************

*********************** CREATE BLOCS **********************************************

foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	gen diff`round'_`bloc'=vs`round'_`bloc'-l3.vs`round'_`bloc'
	}
}

keep year SMD PR *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* diff*
order year SMD PR vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF*

**********************************************************************************
**********************************************************************************
**********************************************************************************

gen AdvantageLibCon=(vs1_LIB-vs1_CON)/(vs1_CON+vs1_LIB)
gen AdvantageLibLab=(vs1_LIB-vs1_LAB)/(vs1_LIB+vs1_LAB)
gen AdvantageLabCon=(vs1_LAB-vs1_CON)/(vs1_LAB+vs1_CON)

foreach var in AdvantageLibCon AdvantageLibLab AdvantageLabCon {
gen lag_`var'=l3.`var'
}

sort SMD
merge SMD using dta/StableSample

sum lag_Advantage* if year==1921 & sample==1
corr lag_Advantage* if year==1921 & sample==1


**********************************************************************************
**********************************************************************************
**********************************************************************************
** add occupation data **
drop _merge
gen districtid=SMD
replace districtid=PR if year>1920

sort year districtid 
merge year districtid using dta/SupplySideAdjustments.dta
keep if _merge==3

xtset SMD year

foreach var in WhiteCON FarmersCON BlueCON OtherCON WhiteLAB FarmersLAB BlueLAB OtherLAB WhiteLIB FarmersLIB BlueLIB OtherLIB {
gen diff_`var'=`var'-l3.`var'
}

**********************************************************************************
*********************Regression analysis of 1921 *********************************
*********************************************************************************

eststo clear
reg diff1_LIB lag_AdvantageLibCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_LAB lag_AdvantageLibCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_CON lag_AdvantageLibCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_OTH lag_AdvantageLibCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_LIB lag_AdvantageLibLab diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_LAB lag_AdvantageLibLab diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_CON lag_AdvantageLibLab diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_OTH lag_AdvantageLibLab diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_LIB lag_AdvantageLabCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_LAB lag_AdvantageLabCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_CON lag_AdvantageLabCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo 
reg diff1_OTH lag_AdvantageLabCon diff_White* diff_Blue* diff_Farmer* if year==1921 & sample==1, robust
eststo
esttab using tables/TableA3.tex, replace se b(%9.3f) se(%9.3f) nostar r2 mtitles("$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$" "$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$" "$/Delta VS^{LIB}$" "$/Delta VS^{LAB}$" "$/Delta VS^{CON}$" "$/Delta VS^{OTH}$") ///
coeflabels(_cons "Constant" lag_AdvantageLibCon  "Pre-reform $/frac{VS^{LIB}-VS^{CON}}{VS^{LIB}+VS^{CON}}$" lag_AdvantageLibLab "Pre-reform $/frac{VS^{LIB}-VS^{LAB}}{VS^{LIB}+VS^{LAB}}$" lag_AdvantageLabCon "Pre-reform $/frac{VS^{LAB}-VS^{CON}}{VS^{LAB}+VS^{CON}}$" ///
diff_WhiteCON "$/Delta$ White-collar CON" ///
diff_WhiteLAB "$/Delta$ White-collar LAB" ///
diff_WhiteLIB "$/Delta$ White-collar LIB" ///
diff_BlueCON "$/Delta$ Blue-collar CON" ///
diff_BlueLAB "$/Delta$ Blue-collar LAB" ///
diff_BlueLIB "$/Delta$ Blue-collar LIB" ///
diff_FarmersCON "$/Delta$ Farmers CON" ///
diff_FarmersLAB "$/Delta$ Farmers LAB" ///
diff_FarmersLIB "$/Delta$ Farmers LIB") ///
order(lag_AdvantageLibCon lag_AdvantageLibLab lag_AdvantageLabCon diff_WhiteCON diff_WhiteLAB diff_WhiteLIB diff_BlueCON diff_BlueLAB diff_BlueLIB diff_FarmersCON diff_FarmersLAB diff_FarmersLIB) ///
nonotes prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")  
